Skip to content

Take the node id from the library when deleting a node - #556

Merged
paceholder merged 3 commits into
masterfrom
fix-delete-node-id
Nov 23, 2026
Merged

paceholder merged 3 commits into
masterfrom
fix-delete-node-id

Conversation

@paceholder

Copy link
Copy Markdown
Owner

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation/refactoring

Description

Deleting a node in the UI called deleteNode(0) when the model kept the default saveNode(). The undo commands took the node id from the JSON that saveNode() returns. The default returns {}, so the id was missing and became 0. The wrong node was deleted, undo could not restore the node, and paste crashed.

The commits are in this order, and each one builds and passes on its own:

  1. Take the node id from the library, not from saveNode. The library writes "id" itself and checks for missing graphics objects on insert. The docs now say what saveNode() and loadNode() are for: without them undo of a delete cannot restore the node.
  2. Test delete, undo and paste of nodes through the undo stack. New file TestSerializedItemCommands.cpp: 9 coverage cases and 3 regression cases. Against the old source, the paste case crashes (SIGSEGV) and the delete case fails with {0} == {6}.
  3. Read node ids from JSON as 64-bit numbers. QJsonValue::toInt() returns 0 for a number above the int range, so an id of 2^31 or more was lost in the same way. The two new tests use ids from 3000000000 and fail on the source of commit 2 with {0} == {3000000001}. It is separate so it can be dropped alone.

saveNode() and loadNode() stay as the undo hook in 3.x. The redesign is tracked in #554. The same toInt() problem in scene load and group restore is fixed in the follow-up PR.

Testing

  • Qt version tested: Qt 6.12.0, macOS arm64 (Apple Clang), offscreen platform
  • Existing tests still pass (885 assertions in 83 test cases at the last commit)
  • Added tests for new functionality (see commit 2 and 3)
  • Not tested: Qt 5, Windows. CI covers them.

Breaking changes?

No. A model that already writes "id" in saveNode() still works.

Related issue

Fixes #490
Related: #554

The undo commands read the node id from the JSON that saveNode()
returns. The default saveNode() returns an empty object. toInt() of a
missing key is 0, and 0 is a valid node id (InvalidNodeId is the
maximum value). So nothing reported the error. A UI delete called
deleteNode(0) and left the selected node in place. Undo and paste then
used a null graphics object.

The contract that the JSON must hold the id was never written down.
Every example and TestGraphModel override saveNode(), so the default
path never ran.

The library now sets "id" on every saveNode() result. It also skips a
node or connection that the model did not restore, instead of using a
null graphics object. The docs say that undo of a delete needs
saveNode() and loadNode(). With the defaults the node is deleted
correctly, undo does not restore it and nothing crashes.

The tests in the next commit fail on the code before this commit. With
the default saveNode(), delete calls deleteNode(0). Undo of a delete
and paste crash with SIGSEGV.

Fixes #490
Cover DeleteCommand, CreateCommand, CopyCommand and PasteCommand with a
model that implements saveNode() and loadNode(). The test model starts
its node ids at 5 and the tests delete a middle node, so that a wrong
id 0 cannot hide a bug. A subclass records the ids that reach
deleteNode().

Add cases for a model that keeps the default saveNode() and loadNode()
(issue 490). They pass with the previous commit. Without it, delete
calls deleteNode(0), and undo of a delete and paste crash with SIGSEGV.
The undo commands read node ids with QJsonValue::toInt(). For a number
above the int range, toInt() returns its default value. A node id of
2^31 or more became 0 or InvalidNodeId. This is the same failure as in
the missing-id case: delete called deleteNode(0), and undo and paste
lost the node. Ids are written as qint64, and NodeId is unsigned.

Add jsonValueToUInt32() and use it in the undo commands and in
fromJson() for connection ids. The test model now writes and reads ids
as 64-bit values too.

The new tests use a model whose ids start at 3000000000. They fail
without this change.
@paceholder
paceholder merged commit 27c92b4 into master Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nodeId parameter is always 0 when read from virtual bool deleteNode(NodeId const nodeId) override

1 participant